public class 地图分析 {
    public static int MAXN = 101;
    public static int MAXM = 101;
    public static int[][] queue = new int[MAXN * MAXM][2];
    public static int l;
    public static int r;
    public static boolean[][] vis = new boolean[MAXN][MAXM];
    public static int[] move = new int[]{-1,0,1,0,-1};
    public static int maxDistance(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        l = r = 0;
        int seas = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(grid[i][j] == 1){
                    //将所有陆地都添加
                    queue[r][0] = i;
                    queue[r++][1] = j;
                    vis[i][j] = true;
                }else {
                    vis[i][j] = false;//诡异的是力扣上不加这个无法提交
                    seas++;
                }
            }
        }
        if(seas == 0 || seas == n * m){
            return -1;
        }
        int level = 0;
        while(l < r){
            level++;
            int size = r - l;
            while(size-- > 0){
                int a = queue[l][0];
                int b = queue[l++][1];
                for (int i = 0; i < 4; i++) {
                    int x = a + move[i];
                    int y = b + move[i + 1];
                    if(x >= 0 && y >= 0 && x < n && y < m && !vis[x][y]){
                        vis[x][y] = true;
                        queue[r][0] = x;
                        queue[r++][1] = y;
                    }
                }
            }
        }
        return level - 1;
    }
}
